package com.objectproject.main.dao;

import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.objectproject.main.dao.iface.MyPropertyDAO;
import com.objectproject.main.dao.mybatis.mapper.MyPropertyMapper;
import com.objectproject.main.model.MyObject;
import com.objectproject.main.model.MyProperty;
import com.objectproject.main.service.subsystem.MyLocaleResolver;

@Repository
public class MyPropertyDAOImpl implements MyPropertyDAO {

	private static final Logger logger = LoggerFactory
			.getLogger(MyPropertyDAOImpl.class);

	@Autowired
	MyObjectDAOImpl myObjectDAOImpl;

	@Autowired
	MyLocaleResolver myLocaleResolver;

	@Autowired
	MyPrimitiveDAOImpl myPrimitiveDAOImpl;

	@Autowired
	SqlSession sqlSession;

	@Override
	public MyProperty selectMyPropertyOfById(long objectId, long propertyId) {
		logger.info("Method invocation : selectMyPropertyById");
		return sqlSession.getMapper(MyPropertyMapper.class).selectMyPropertyOfById(objectId, propertyId);
	}

	@Override
	public MyProperty selectMyPropertyDetailOfById(long objectId,
			long propertyId, int localeId) {
		
		return sqlSession.getMapper(MyPropertyMapper.class).selectMyPropertyDetailOfById(objectId, propertyId);
	}
	
	@Override
	public long insertMyProperty(MyObject myObject, MyProperty myProperty) {

		logger.info("Method invocation : insertMyProperty");


		//오버라이딩 하는 경우
		if(myProperty.getPropertyId() != 0){
			
			sqlSession.getMapper(MyPropertyMapper.class).overridedInsertMyProperty(myObject, myProperty);
			
		}else{//오버라이딩하지 않는 경우
			
			sqlSession.getMapper(MyPropertyMapper.class).insertMyProperty(myObject, myProperty);
		}
		
		return myProperty.getPropertyId();

	}

	@Override
	public long insertMyPropertyLocaleSpecific(MyObject myObject,
			MyProperty myProperty) {

		logger.info("Method invocation : insertMyPropertyLocaleSpecific");

		sqlSession.getMapper(MyPropertyMapper.class)
				.insertMyPropertyLocaleSpecific(myObject, myProperty);

		return myProperty.getPropertyId();
	}

	@Override
	public void insertMyPropertyDetail(MyObject myObject,
			MyProperty myProperty, int localeId) {

		logger.info("Method invocation : insertMyPropertyDetail");
		
		sqlSession.getMapper(MyPropertyMapper.class).insertMyPropertyDetail(
				myObject, myProperty, localeId);

	}

	@Override
	public void deleteMyPropertyById(long propertyId) {
		sqlSession.getMapper(MyPropertyMapper.class).deleteMyPropertyById(propertyId);
	}

	@Override
	public void deleteMyPropertyOfById(long objectId, long propertyId) {
		sqlSession.getMapper(MyPropertyMapper.class).deleteMyPropertyOfById(objectId, propertyId);
	}

	@Override
	public void deleteMyProperty(MyProperty myProperty) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteMyPropertyOf(MyObject myObject, MyProperty myProperty) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateMyProperty(MyObject myObject, MyProperty myProperty) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateMyPropertyDetail(MyObject myObject,
			MyProperty myProperty, int localeId) {
		sqlSession.getMapper(MyPropertyMapper.class).updateMyPropertyDetail(
				myObject, myProperty, localeId);
	}

	// advanced

	@Override
	public ArrayList<MyProperty> selectMyProperties(MyObject myObject) {
		return sqlSession.getMapper(MyPropertyMapper.class).selectMyProperties(
				myObject);
	}

	@Override
	public void insertMyProperties(MyObject myObject,
			ArrayList<MyProperty> myProperties) {
		// 반복할 때는 함수콜 횟수를 줄이기위해 직접 매퍼를 호출한다.
		
		
		logger.info("Method invocation : insertMyProperties");
		MyProperty currentProperty;
		
		
		if (myProperties != null) {
			for (int i = 0; i < myProperties.size(); i++) {
				currentProperty = myProperties.get(i);
				
				//오버라이딩 하는 경우
				if(currentProperty.getPropertyId() != 0){
					
					sqlSession.getMapper(MyPropertyMapper.class).overridedInsertMyProperty(myObject, currentProperty);
					
				}else{//오버라이딩하지 않는 경우
					
					sqlSession.getMapper(MyPropertyMapper.class).insertMyProperty(myObject, currentProperty);
				}
			}
		}
	}

	@Override
	public void deleteMyProperties(MyObject myObject) {
		logger.info("Method invocation : deleteMyProperties");

		sqlSession.getMapper(MyPropertyMapper.class).deleteMyProperties(
				myObject);

	}

	@Override
	public void insertMyPropertyDetails(MyObject myObject, ArrayList<MyProperty> myProperties, int localeId) {
		// 반복할 때는 함수콜 횟수를 줄이기위해 직접 매퍼를 호출한다.
		logger.info("Method invocation : insertMyPropertyDetails");
		if (myProperties != null) {
			for (int i = 0; i < myProperties.size(); i++) {
				
				sqlSession.getMapper(MyPropertyMapper.class).insertMyPropertyDetail(myObject, myProperties.get(i), localeId);
			}
		}
	}

	@Override
	public MyProperty semanticSelectMyPropertyOfById(long objectId, long propertyId) {
		
		return sqlSession.getMapper(MyPropertyMapper.class).semanticSelectMyPropertyOfById(objectId, propertyId);
	}

	@Override
	public void protectedDeleteMyPropertyById(long propertyId) {
		sqlSession.getMapper(MyPropertyMapper.class).protectedDeleteMyPropertyById(propertyId);
	}

	@Override
	public void protectedDeleteMyPropertyOfById(long objectId, long propertyId) {
		sqlSession.getMapper(MyPropertyMapper.class).protectedDeleteMyPropertyOfById(objectId, propertyId);
	}

	@Override
	public void protectedDeleteMyProperty(MyProperty myProperty) {
		sqlSession.getMapper(MyPropertyMapper.class).protectedDeleteMyProperty(myProperty);
	}

	@Override
	public void protectedDeleteMyPropertyOf(MyObject myObject, MyProperty myProperty) {
		logger.info("Method invocation : protectedDeleteMyPropertyOf");
		sqlSession.getMapper(MyPropertyMapper.class).protectedDeleteMyPropertyOf(myObject, myProperty);
	}

	@Override
	public ArrayList<MyProperty> semacticSelectMyProperties(MyObject myObject) {
		logger.info("Method invocation : semacticSelectMyProperties");
		return sqlSession.getMapper(MyPropertyMapper.class).semanticSelectMyProperties(myObject);
	}

	@Override
	public void protectedDeleteMyProperties(MyObject myObject) {
		logger.info("Method invocation : protectedDeleteMyProperties");
		sqlSession.getMapper(MyPropertyMapper.class).protectedDeleteMyProperties(myObject);
	}

	@Override
	public MyProperty semanticSelectMyPropertyOf(MyObject myObject, MyProperty myProperty) {
		logger.info("Method invocation : semanticSelectMyPropertyOf");
		return sqlSession.getMapper(MyPropertyMapper.class).semanticSelectMyPropertyOf(myObject, myProperty);
	}

	@Override
	public MyProperty selectMyPropertyOf(MyObject myObject, MyProperty myProperty) {
		logger.info("Method invocation : selectMyPropertyOf");
		return sqlSession.getMapper(MyPropertyMapper.class).selectMyPropertyOf(myObject, myProperty);
	}

	@Override
	public MyProperty selectMyPropertyDetailOf(MyProperty myProperty, int localeId) {
		logger.info("Method invocation : selectMyPropertyDetailOf");
		return sqlSession.getMapper(MyPropertyMapper.class).selectMyPropertyDetailOf(myProperty, localeId);
	}

	@Override
	public long overridedInsertMyProperty(MyObject myObject,
			MyProperty myProperty) {
		logger.info("Method invocation : overridedInsertMyProperty");

		sqlSession.getMapper(MyPropertyMapper.class).overridedInsertMyProperty(myObject,
				myProperty);

		return myProperty.getPropertyId();
	}

	@Override
	public void updateMyPropertyValue(MyObject myObject, MyProperty myProperty, int localeId) {
		logger.info("Method invocation : updateMyPropertyValue");

		sqlSession.getMapper(MyPropertyMapper.class).updateMyPropertyValue(myObject, myProperty, localeId);
	}

}
